/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Text;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Microsoft.Practices.EnterpriseLibrary.Data;
using ELE.EnterpriseLibrary.Data;

namespace ELE.EnterpriseLibrary.Data.Oracle.Test
{
  /// <summary>
  /// Summary description for OracleDatabaseTest
  /// </summary>
  [TestClass]
  public class OracleDatabaseTest
  {
    private Database database;

    public OracleDatabaseTest()
    {
      database = DatabaseFactory.CreateDatabase();
    }

    protected void EnsureDatabase()
    {
      if (database == null)
      {
        database = DatabaseFactory.CreateDatabase();
        if (database == null)
        {
          throw new ApplicationException("database is null");
        }
      }
    }
    protected IObjectQueryRunner GetStorageRunner()
    {
      ObjectStorage store = ObjectStorageFactory.CreateStorage();
      if (store == null)
      {
        throw new ApplicationException("Failed to initialize object storage");
      }
      return store.GetQueryRunner();
    }
    protected Encounter GetNewEncounter(int id)
    {
      Encounter enc = new Encounter();
      enc.Id = id;
      return enc;
    }

    #region Additional test attributes
    //
    // You can use the following additional attributes as you write your tests:
    //
    // Use ClassInitialize to run code before running the first test in the class
    // [ClassInitialize()]
    // public static void MyClassInitialize(TestContext testContext) { }
    //
    // Use ClassCleanup to run code after all tests in a class have run
    // [ClassCleanup()]
    // public static void MyClassCleanup() { }
    //
    // Use TestInitialize to run code before running each test 
    // [TestInitialize()]
    // public void MyTestInitialize() { }
    //
    // Use TestCleanup to run code after each test has run
    // [TestCleanup()]
    // public void MyTestCleanup() { }
    //
    #endregion

    [TestMethod]
    public void TestConnection()
    {
      this.EnsureDatabase();

      DataSet set = database.ExecuteDataSet(database.GetSqlStringCommand("select * from gdn_encounter where rownum < 5"));      
      if (set == null)
      {
        throw new ApplicationException("Query failed");
      }

    }
    [TestMethod]
    public void TestQuery()
    {
      this.EnsureDatabase();

      Encounter inputObject = this.GetNewEncounter(3452);
      IObjectQueryRunner storageQueryRunner = this.GetStorageRunner();
      try
      {
        DataSet set = storageQueryRunner.ExecuteDatasetQueryByName(database, "GetEncounters", inputObject);
        if (set == null || set.Tables.Count <= 0)
        {
          throw new ApplicationException("Failed to run dataset mapped query GetEncounters");
        }

        IList storedObjects = storageQueryRunner.ExecuteQueryByName(database, "GetEncounters", inputObject);
        if (storedObjects == null || storedObjects.Count <= 0)
        {
          throw new ApplicationException("Failed to run mapped query GetEncounters");
        }

      }
      catch (Exception ex)
      {
        throw new ApplicationException("Failed to run ORM", ex);
      }

    }
    [TestMethod]
    public void TestMapping()
    {
      this.EnsureDatabase();

      Encounter inputObject = this.GetNewEncounter(3452);
      IObjectQueryRunner storageQueryRunner = this.GetStorageRunner();
      try
      {
        Encounter encounter = (Encounter)storageQueryRunner.QueryObjectByPrimaryKey(database, "Encounter", inputObject.Id);
      }
      catch (Exception ex)
      {
        throw new ApplicationException("Failed to run ORM", ex);
      }
    }
    [TestMethod]
    public void TestParser()
    {
      OracleSqlParser parser = new OracleSqlParser();
      List<string> parsed = parser.GetInputParameterNames("select * from a where b = :Id and c = :Exist");
      Console.WriteLine(parsed.ToString());
    }
  }
}
